package ECB16S1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ECB {
	public static final String TOKENLIST[] = {"phone","birthday","name","email","address"};
	public static void main(String args[]) throws IOException{
		
		if(args.length<4){
			System.out.println("Invalid Args");
		}else{
			String phone = args[0];
			String instruction = args[1];
			String output = args[2];
			String report = args[3];
			BufferedReader reader = new BufferedReader(new FileReader(new File(phone)));
			String line = "";
			List<Phonebook> list = new ArrayList<Phonebook>();//hold phonebook list
			List<ArrayList<Phonebook>> queryLists = new ArrayList<ArrayList<Phonebook>>();
			Phonebook phonebook = new Phonebook();
			while( (line  = reader.readLine())!=null){
				if(line.trim().startsWith(TOKENLIST[0])){//phone
					String rawPhone = line.substring(line.indexOf(TOKENLIST[0])+TOKENLIST[0].length()).trim();
					phonebook.setPhone(Integer.parseInt(rawPhone)+"");//remove leading zeros
				}else
				if(line.trim().startsWith(TOKENLIST[1])){//birthday
					String birthday = line.substring(line.indexOf(TOKENLIST[1])+TOKENLIST[1].length()).trim();
					phonebook.setBirthday(birthday);
				}else
				if(line.trim().startsWith(TOKENLIST[2])){//name
					String name = line.substring(line.indexOf(TOKENLIST[2])+TOKENLIST[2].length()).trim();
					phonebook.setBirthday(name);
				}else
				if(line.trim().startsWith(TOKENLIST[3])){//email
					String email = line.substring(line.indexOf(TOKENLIST[3])+TOKENLIST[3].length()).trim();
					phonebook.setEmail(email);
				}else
				if(line.trim().startsWith(TOKENLIST[4])){//address
					String address = line.substring(line.indexOf(TOKENLIST[4])+TOKENLIST[4].length()).trim();
					phonebook.setBirthday(address);
				}else
				if(line.trim().equals("")){//end of a record
					list.add(phonebook);
					phonebook = new Phonebook();
				}else{//multi-line address 
					String address = line.trim();
					phonebook.setAddress(phonebook.getAddress()+" "+address);
				}
			}
			reader.close();
			reader = new BufferedReader(new FileReader(new File(instruction)));//read instructions
			while( (line  = reader.readLine())!=null){
				if(line.trim().startsWith("add")){
					line = line.substring(line.indexOf("add")+3);
					String s[] = line.trim().split(";");
					Phonebook temp = new Phonebook();
					for(String ss : s){
						if(ss.trim().startsWith(TOKENLIST[0])){//phone
							String tphone = ss.trim().substring(ss.indexOf(TOKENLIST[0])+TOKENLIST[0].length()).trim();
							temp.setPhone(tphone);
						}
						if(ss.trim().startsWith(TOKENLIST[1])){//birtday
							String tbirthday = ss.trim().substring(ss.indexOf(TOKENLIST[1])+TOKENLIST[1].length()).trim();
							temp.setBirthday(tbirthday);
						}
						if(ss.trim().startsWith(TOKENLIST[2])){//name
							String tname = ss.trim().substring(ss.indexOf(TOKENLIST[2])+TOKENLIST[2].length()).trim();
							temp.setName(tname);
						}
						if(ss.trim().startsWith(TOKENLIST[3])){//email
							String temail = ss.trim().substring(ss.indexOf(TOKENLIST[3])+TOKENLIST[3].length()).trim();
							temp.setEmail(temail);
						}
						if(ss.trim().startsWith(TOKENLIST[4])){//address
							String temail = ss.trim().substring(ss.indexOf(TOKENLIST[4])+TOKENLIST[4].length()).trim();
							temp.setEmail(temail);
						}
						for(Phonebook pb : list){
							//find and update
							if(pb.getName().equals(temp.getName())&&pb.getBirthday().equals(temp.getBirthday())){
								pb.setAddress(temp.getAddress());
								pb.setEmail(temp.getEmail());
								pb.setPhone(temp.getPhone());
							}else{
								list.add(temp);//can not find then add
							}
						}
					}
				}
				if(line.trim().startsWith("delete")){
					line = line.substring(line.indexOf("delete")+6);
					String s[] = line.trim().split(";");
					String birthday = "";
					String name = "";
					for(String ss : s){
						if(ss.trim().startsWith(TOKENLIST[1])){//birtday
							birthday = ss.trim().substring(ss.indexOf(TOKENLIST[1])+TOKENLIST[1].length()).trim();
						}
						if(ss.trim().startsWith(TOKENLIST[2])){//name
							name = ss.trim().substring(ss.indexOf(TOKENLIST[2])+TOKENLIST[2].length()).trim();
						}
						List<Integer> deletePos = new ArrayList<Integer>();
						for(int i=0;i<list.size();i++){
							//find and mark
							Phonebook pb = list.get(i);
							if(!birthday.equals("")&&!name.equals("")){
								if(pb.getName().equals(name)&&pb.getBirthday().equals(birthday)){
									deletePos.add(i);
								}
							}
							if(!birthday.equals("")&&name.equals("")){
								if(pb.getBirthday().equals(birthday)){
									deletePos.add(i);
								}
							}
							if(birthday.equals("")&&!name.equals("")){
								if(pb.getName().equals(name)){
									deletePos.add(i);
								}
							}
						}
						//delete
						for(int i:deletePos){
							list.remove(i);
						}
					}
				}
				if(line.trim().startsWith("save")){
					BufferedWriter writer = new BufferedWriter(new FileWriter(new File(output)));
					for(Phonebook pb : list){
						writer.append("name"+" "+pb.getName());
						writer.newLine();
						writer.append("birthday"+" "+pb.getBirthday());
						writer.newLine();
						writer.append("phone"+" "+pb.getPhone());
						writer.newLine();
						writer.append("email"+" "+pb.getEmail());
						writer.newLine();
						writer.append("address"+" "+pb.getAddress());
						writer.newLine();
						writer.newLine();
					}
					writer.close();
				}
				if(line.trim().startsWith("query")){
					line = line.substring(line.indexOf("query")+5).trim();
					String tphone="",tbirthday="",tname="";
					if(line.startsWith(TOKENLIST[0])){//phone
						tphone =line.substring(line.indexOf(TOKENLIST[0])+TOKENLIST[0].length()).trim();
					}
					if(line.startsWith(TOKENLIST[1])){//birtday
						tbirthday  = line.substring(line.indexOf(TOKENLIST[1])+TOKENLIST[1].length()).trim();
					}
					if(line.startsWith(TOKENLIST[2])){//name
						tname  = line.substring(line.indexOf(TOKENLIST[2])+TOKENLIST[2].length()).trim();
					}
					List<Phonebook> resultList = new ArrayList<Phonebook>();
					for(Phonebook pb : list){
						if(tphone!=null&&tphone.equals(pb.getPhone())){
							resultList.add(pb);
						}
						if(tbirthday!=null&&tbirthday.equals(pb.getBirthday())){
							resultList.add(pb);
						}
						if(tname!=null&&tname.equals(pb.getName())){
							resultList.add(pb);
						}
					}
					
				}
			}
			reader.close();
		}
	}
	public static List<Phonebook> InsertSort(List<Phonebook> arr){
	  int i,j;
	  Phonebook insertNote;
	  List<Phonebook> array=arr;
	    
	  for (i=1;i<array.size();i++)
	  {
	   insertNote = array.get(i);
	   j=i-1;
	   while(j>=0&&(insertNote.getName().compareTo(array.get(j).getName())<0))   
	   {
	    array[j+1]=array[j];
	    j--;
	   }
	   array[j+1]=insertNote; 
	  }
	  return array;
	  
	}
	public boolean lessThan(){
		
	}
}
class Phonebook{
	private String name;
	private String birthday;
	private String phone;
	private String address;
	private String email;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Phonebook(String name, String birthday, String phone, String address,String email) {
		super();
		this.name = name;
		this.birthday = birthday;
		this.phone = phone;
		this.address = address;
		this.email = email;
	}
	public Phonebook(){
		
	}
	
}